#!/usr/bin/env ruby

KEY2TYPE = {
  "user_time"                           => "lat",
  "system_time"                         => "lat",
  "percent_of_cpu_this_job_got"         => "percent",
  "elapsed_time"                        => "lat",
  "average_shared_text_size"            => "mem",
  "average_unshared_data_size"          => "mem",
  "average_stack_size"                  => "mem",
  "average_total_size"                  => "mem",
  "maximum_resident_set_size"           => "mem",
  "average_resident_set_size"           => "mem",
  "major_page_faults"                   => "cnt",
  "minor_page_faults"                   => "cnt",
  "voluntary_context_switches"          => "cnt",
  "involuntary_context_switches"        => "cnt",
  "swaps"                               => "cnt",
  "file_system_inputs"                  => "cnt",
  "file_system_outputs"                 => "cnt",
  "socket_messages_sent"                => "cnt",
  "socket_messages_received"            => "cnt",
  "signals_delivered"                   => "cnt",
  "page_size"                           => "param",
  "exit_status"                         => "eid",
}

while (line = $stdin.gets)
  next if line.index('Command being timed: ')

  key, val = line.split ': '

  # skip the first line to avoid empty val.
  # Command exited with non-zero status 2
  #   Command being timed: "/lkp/wfg/src/tests/kernel-selftests"
  #   User time (seconds): 13.70
  #   ...
  next unless val

  key = key.gsub(/^\s+/, '').gsub(/ \([^)]+\)/, '').gsub(/\s+/, '_').downcase
  case key
  when 'elapsed_time'
    times = val.split ':'
    val = times.inject(0) do |tt, t|
      tt * 60 + t.to_f
    end
  when 'percent_of_cpu_this_job_got'
    val.chomp!("%\n")
    val = '0' if val == '?'
  end
  puts "#{KEY2TYPE[key]}.#{key}: #{val}"
end
